rm(list=ls())
require("rgdal")
library(rgdal)
library(tmap)
require("ggplot2")
require("plyr")
require("dplyr")

setwd("/Users/Mandywu/Dropbox (MIT)/PNAS revision/code")

#read in spatial data
area <- readOGR("/Users/Mandywu/Dropbox (MIT)/PNAS revision/code/CHN_adm1.shp")

area@data$NAME_1 <- as.character(area@data$NAME_1)
area@data[area@data$NAME_1 == "Nei Mongol", "NAME_1"] <- "Inner Mongolia"
area@data[area@data$NAME_1 == "Xizang", "NAME_1"] <- "Tibet"
area@data[area@data$NAME_1 == "Hebei", "NAME_1"] <- " "
area@data[area@data$NAME_1 == "Beijing", "NAME_1"] <- ""
area@data[area@data$NAME_1 == "Tianjin", "NAME_1"] <- "  "

inspection <- as.data.frame(matrix(nrow=31,ncol=3))
names(inspection) <- c("province_name","NAME_1","batch")
inspection$province_name <- c("上海市","云南省","内蒙古自治区","北京市","吉林省","四川省","天津市",          
                              "宁夏回族自治区","安徽省","山东省","山西省","广东省","广西壮族自治区","新疆维吾尔自治区",
                              "江苏省","江西省","河北省","河南省","浙江省","海南省","湖北省",
                              "湖南省","甘肃省","福建省","西藏自治区","贵州省","辽宁省",
                              "重庆市","陕西省","青海省","黑龙江省")

inspection$NAME_1  <- c("Shanghai","Yunnan","Inner Mongolia","","Jilin","Sichuan","  ","Ningxia Hui","Anhui",
                                    "Shandong","Shanxi","Guangdong","Guangxi","Xinjiang Uygur","Jiangsu","Jiangxi"," ","Henan",
                                    "Zhejiang","Hainan","Hubei","Hunan","Gansu","Fujian","Tibet","Guizhou","Liaoning","Chongqing",
                                    "Shaanxi","Qinghai","Heilongjiang")

inspection[inspection$province_name=="河北省",]$batch <- "Trial round 2015/12/31 - 2016/02/14"

inspection[inspection$province_name=="内蒙古自治区"|inspection$province_name=="宁夏回族自治区"|inspection$province_name=="广西壮族自治区"|
             inspection$province_name=="黑龙江省"|inspection$province_name=="江苏省"|inspection$province_name=="江西省"|
             inspection$province_name=="河南省"|inspection$province_name=="云南省",]$batch <- "Round 1 2016/7/12 - 2016/8/19"
inspection[inspection$province_name=="北京市"|inspection$province_name=="上海市"|inspection$province_name=="重庆市"|
             inspection$province_name=="湖北省"|inspection$province_name=="广东省"|inspection$province_name=="陕西省"|
             inspection$province_name=="甘肃省",]$batch <- "Round 2 2016/11/24 - 2016/12/30"
inspection[inspection$province_name=="天津市"|inspection$province_name=="山西省"|inspection$province_name=="辽宁省"|
             inspection$province_name=="安徽省"|inspection$province_name=="福建省"|inspection$province_name=="湖南省"|
             inspection$province_name=="贵州省",]$batch <- "Round 3 2017/4/24 - 2017/5/28"
inspection[is.na(inspection$batch),]$batch <- "Round 4 2017/8/7 - 2017/9/15"


area_joined <- merge(area, inspection, by.x="NAME_1", by.y="NAME_1")
tm3 <- qtm(area_joined, fill="batch", text="NAME_1", text.size=1.2,format = "World", style = "col_blind",text.scale=1, fill.title="Inspection Rounds",fill.palette="YlOrBr") +
  tm_legend(legend.position = c("left", "bottom"),text.size=1.5,
            main.title = "Inspection Rounds plot",
            main.title.position = "right")
tm3
